package com.dome.network1;

import java.io.*;
import java.net.Socket;

//TCP客户端上传文件案例
public class Client {
    public static void main(String[] args) throws IOException {
        //将本地文件读取后.用网络写入传送给服务端
        //先创建客户端Socket对象,传入IP地址和窗口
        Socket socket=new Socket("192.168.81.224",7777);
        //创建缓冲字节输入流对象,读取本地文件
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("C:\\git-lx\\a.txt"));
        //再创建网络输出流将读取的文件写入
       BufferedOutputStream bos=new BufferedOutputStream(socket.getOutputStream());
        byte[] bytes=new byte[1024];
        int len;
        while ((len=bis.read(bytes))!=-1){
           bos.write(bytes,0,len);
           bos.flush();
        }
        System.out.println("看看我执行了没");
        //禁用此套接字输出流
        socket.shutdownOutput();
        //将读入的资源释放
        bis.close();

        System.out.println("======================");

        //服务端接收到信息,会反馈信息,需要创建字符缓冲输入流接收信息
        InputStream is = socket.getInputStream();
        BufferedReader br=new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }

        socket.close();
    }
}
